package com.tencent.qcloud.tim.uikit.component.goods

import androidx.lifecycle.MutableLiveData
import com.library.common.base.BaseViewModel
import com.library.common.base.MessageModel
import com.library.common.ext.rxHttpRequest
import com.library.common.net.NetUrl
import com.library.common.net.error.msg
import com.library.common.toast.XToast
import rxhttp.IAwait
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.toResponse

class SendGoodsListViewModel : BaseViewModel() {
    val goodlist = MutableLiveData<List<GoodsDto>>()

    fun getGoodsList(content: Any, storeId: Int?, couponId: Int?, sort: String, pageNumber: Int) {
        rxHttpRequest {
            onRequest = {
                goodlist.value  = getCateSearchGoodList(
                    content,
                    storeId,
                    couponId,
                    sort,
                    pageNumber,
                    10
                ).await()
            }
            onError = {
                defUI.msgEvent.postValue(MessageModel(1))
                XToast.normal(it.msg)
            }
            isLoadingType = true
            isRefresh = pageNumber == 1
        }
    }


    fun getCateSearchGoodList(
        content: Any,
        storeId: Int?,
        couponId: Int?,
        sort: String,
        pageNumber: Int,
        pageSize: Int
    ): IAwait<List<GoodsDto>> {
        var apiStr = ""
        apiStr = if (storeId != null && storeId != 0) {
            if (content is String) {
                NetUrl.api_home_shop_goods
            } else {
                NetUrl.api_find_by_store_cate
            }
        } else {
            NetUrl.api_home_shop_goods
        }
        val params = HashMap<String, Any>()
        if (storeId != null && storeId != 0) {
            when (content) {
                is String -> {
                    val productMap = HashMap<String, Any>()
                    productMap["name"] = content
                    if (sort == "featured") {
                        productMap["featured"] = true
                    }
                    params["product"] = productMap
                }
                is Int -> if (content != 0 && content != -1) params["category"] =
                    mapOf(Pair("id", content))
            }
            params["storeId"] = storeId
        } else {
            when (content) {
                is String -> {
                    val productMap = HashMap<String, Any>()
                    if (couponId == null || couponId == 0) productMap["name"] = content
                    if (sort == "featured") {
                        productMap["featured"] = true
                    }
                    params["product"] = productMap
                }
                is Int -> if (content != 0 && content != -1) params["category"] =
                    mapOf(Pair("id", content))
            }
            if (couponId != null && couponId != 0) params["coupon"] = mapOf(Pair("id", couponId))
        }
        val pageMap = HashMap<String, Any>()
        pageMap["page"] = pageNumber
        pageMap["size"] = pageSize
        when (sort) {
            "new" -> {
                pageMap["desc"] = "id"
            }
            "up_price" -> {
                pageMap["asc"] = "sale_price"
            }
            "down_price" -> {
                pageMap["desc"] = "sale_price"
            }
        }
        params["pageable"] = pageMap
        return RxHttp.postJson(apiStr).addAll(params).toResponse()
    }
}